package com.styytech.dadabao.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.styytech.dadabao.R;
import com.styytech.dadabao.bean.Notice;
import com.styytech.dadabao.ui.more.NoticeDetailsActivity;

import java.util.LinkedList;

/**
 * 服务公告适配器.
 */
public class NoticeItemAdapter extends BaseAdapter {

    private LinkedList<Object> mList = null;
    private LayoutInflater mInflater = null;
    ViewHolder holder = null;
    private Context mContext;

    public NoticeItemAdapter(LinkedList<Object> mList, Context context) {
        this.mList = mList;
        this.mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        if (mList == null) {
            return 0;
        }
        return mList.size();
    }

    @Override
    public Object getItem(int arg0) {
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup arg2) {
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(
                    R.layout.item_notice_record, null);
            holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.tv_time = (TextView) convertView.findViewById(R.id.tv_time);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Object obj = mList.get(position);
        final Notice itemData = (Notice) obj;
        final String title = itemData.getTitle();
        final String time = itemData.getPublishTime();
        final String content = itemData.getContent();// 内容html
        String profile = itemData.getProfile();// 内容简介
        holder.tv_title.setText(title);
        holder.tv_time.setText(time);

        profile = profile.replace("年"," 年 ");
        profile = profile.replace("月"," 月 ");
        profile = profile.replace("日"," 日 ");
        profile = profile.replace("时"," 时 ");
        profile = profile.replace("分"," 分 ");

        holder.tv_content.setText(ToDBC(profile));

        convertView.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //点击单个条目
                Intent intent = new Intent();
                intent.setClass(mContext, NoticeDetailsActivity.class);
                intent.putExtra("BEAN", itemData);
                mContext.startActivity(intent);
            }
        });

        return convertView;
    }

    private class ViewHolder {
        private TextView tv_title;
        private TextView tv_time;
        private TextView tv_content;
    }
    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i< c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }if (c[i]> 65280&& c[i]< 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }
}
